package pl.jtb.ctl.tabory.dao.impl.mybatis.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import pl.jtb.ctl.tabory.dao.api.services.IUserRoleService;
import pl.jtb.ctl.tabory.dao.data.vo.UserRoleVO;
import pl.jtb.ctl.tabory.dao.impl.mybatis.base.BaseDaoService;

public class UserRoleServiceImpl extends BaseDaoService<UserRoleVO> implements IUserRoleService {

    private final Logger logger = Logger.getLogger(UserRoleServiceImpl.class);

    private final static String MAPPER = "pl.jtb.ctl.taboty.UserRoleMapper";
    private final static String SELECT_ALL_USER_ROLES = ".selectAllUserRoles";
    private final static String SELECT_USER_ROLE = ".selectUserRole";

    public UserRoleServiceImpl(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
    }

    @Override
    public List<UserRoleVO> getAllUserRoles() throws Exception {
        logger.debug("Getting all user roles");
        List<UserRoleVO> result = null;
        try {
            result = selectList(MAPPER + SELECT_ALL_USER_ROLES, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        return result;
    }

    @Override
    public UserRoleVO getUserRole(Long userRoleId) throws Exception {
        logger.debug("Getting user role for id: " + userRoleId);
        UserRoleVO result = null;
        try {
            result = selectOne(MAPPER + SELECT_USER_ROLE, userRoleId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        return result;
    }

}
